package xin.vote.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import xin.vote.model.MatchPKInfo;
import xin.vote.model.Matchs;
import xin.vote.service.IMatchPKInfoService;
import xin.vote.service.IMatchsService;
import xin.vote.util.Result;

@Controller
@RequestMapping("/matchpk")
public class MatchPKInfoController {
	@Resource
	private IMatchPKInfoService matchpkinfoservice;
	@Resource
	private IMatchsService matchsservice;

	//动态查询所有比赛、顶部查询
	@RequestMapping("/getallmatchpkinfodoc")
	@ResponseBody
	public Result findAllMatchePKInfodoc(@RequestParam(value = "matchid", required = false) Integer matchid) {
		
		MatchPKInfo mpk = new MatchPKInfo();
		mpk.setMatchid(matchid);
		
		List<MatchPKInfo> matchpkList = matchpkinfoservice.findAllMatchPKInfodmc(mpk);
		
		Result result = new Result();
		result.setCode(0);
		result.setMsg("成功");
		result.setCount(matchpkList.size());
		result.setData(matchpkList);
		return result;
	}

	//删除对战信息
	@RequestMapping("/deletematchpk")
	@ResponseBody
	public String deleteMatchePKInfo(@RequestParam(value = "matchid", required = false) Integer matchid,
									 @RequestParam(value = "battleid", required = false) String battleid) {

		MatchPKInfo mpk = new MatchPKInfo();
		mpk.setMatchid(matchid);
		mpk.setBattleid(battleid);

		int ret = matchpkinfoservice.deleteMatchPKInfoByPrimaryKey(mpk);
		if (ret > 0) {
			return "1";
		} else {
			return "0";
		}
	}
	
	//下拉框准备数据
	@RequestMapping("/getallmatchname")
	@ResponseBody
	public List<Matchs> findAllMatchName() {
		
		List<Matchs> matchList = matchsservice.selectAllMatches();
		return matchList;
	}
	
	//添加对战情况
	@RequestMapping("/addmatchpk")
	@ResponseBody
	public String addMatchePKInfo(@RequestParam(value = "amatchname", required = false) Integer matchid,
								  @RequestParam(value = "battleid", required = false) String battleid,
								  @RequestParam(value = "playername1", required = false) Integer firstplayerid,
								  @RequestParam(value = "playername2", required = false) Integer secondplayerid,
								  @RequestParam(value = "battleflag", required = false) Byte battleflag) {

		MatchPKInfo mpk = new MatchPKInfo();
		mpk.setMatchid(matchid);
		mpk.setBattleid(battleid);
		mpk.setFirstplayerid(firstplayerid);
		mpk.setSecondplayerid(secondplayerid);
		mpk.setBattleflag(battleflag);

		int ret = matchpkinfoservice.addMatchPKInfoSelective(mpk);
		if (ret > 0) {
			return "1";
		} else {
			return "0";
		}
	}

	//编辑对战情况
	@RequestMapping("/updatematchpkdynamic")
	@ResponseBody
	public String updateMatchePKInfo(@RequestParam(value = "amatchname", required = false) Integer matchid,
								  @RequestParam(value = "battleid", required = false) String battleid,
								  @RequestParam(value = "playername1", required = false) Integer firstplayerid,
								  @RequestParam(value = "firstsongname", required = false) String firstsongname,
								  @RequestParam(value = "playername2", required = false) Integer secondplayerid,
								  @RequestParam(value = "secondsongname", required = false) String secondsongname,
								  @RequestParam(value = "battleflag", required = false) Byte battleflag) {

		MatchPKInfo mpk = new MatchPKInfo();
		mpk.setMatchid(matchid);
		mpk.setBattleid(battleid);
		mpk.setFirstplayerid(firstplayerid);
		mpk.setFirstsongname(firstsongname);
		mpk.setSecondplayerid(secondplayerid);
		mpk.setSecondsongname(secondsongname);
		mpk.setBattleflag(battleflag);

		int ret = matchpkinfoservice.updateByPrimaryKeySelective(mpk);
		if (ret > 0) {
			return "1";
		} else {
			return "0";
		}
	}
}
